use actix_web::{
    App, HttpServer,
    web::{self, ServiceConfig},
};
use student_grade_manage_system::{
    handlers::{logon_handler, student_handler::students},
    middleware::auth_middleware::AuthMiddleware,
};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .configure(anonymous_services)
            .configure(auth_services)
        // .service(students)
    })
    .bind(("127.0.0.1", 8000))?
    .run()
    .await
}

fn anonymous_services(cfg: &mut ServiceConfig) {
    cfg.service(logon_handler::logon);
    // cfg.service(web::scope("/logon").route("",web::post().to(logon_handler::logon)));
}

fn auth_services(cfg: &mut ServiceConfig) {
    let scope = web::scope("/api").wrap(AuthMiddleware).service(students);
    cfg.service(scope);
}
